<template>
  <div class="case">
    <na-table
      :datas.sync="tableData"
      :searchObj.sync="search"
      @updateDatas="getAllLists"
      @newAdd="handleOtherInstockBill"
    >
    </na-table>
    <!-- 新建供应商核价 -->
    <el-dialog
      :title="'供应商核价-' + modelTitle"
      :visible.sync="isAddPurDialog"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
      @close="closeDialog"
    >
      <div class="dialogDody">
        <div class="addWuliaoBox">
          <el-form
            ref="inStockForm"
            :model="inStockForm"
            :rules="rules"
            label-width="80px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item
                  label="核价单号"
                  prop="goodsPriceAdjustmentNumber"
                >
                  <el-input
                    v-model="inStockForm.goodsPriceAdjustmentNumber"
                    placeholder="保存后自动生成"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="创建人员" prop="associationBillNo">
                  <el-input
                    :value="inStockForm.createorName || name"
                    placeholder="创建人员"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="创建日期" prop="associationBillNo">
                  <el-input
                    :value="inStockForm.createTime"
                    placeholder="创建日期"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="供应商" prop="supplierCode">
                  <el-select
                    v-model="inStockForm.supplierCode"
                    placeholder="请选择供应商"
                    filterable
                    :disabled="disabled"
                    size="mini"
                    @change="supplierChange"
                  >
                    <el-option
                      :key="item.supplierID"
                      :label="item.supplierName"
                      :value="item.supplierCode"
                      v-for="(item, index) in allSupliers"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="生效日期" prop="startTime">
                  <el-date-picker
                    v-model="inStockForm.startTime"
                    value-format="yyyy-MM-dd"
                    :disabled="disabled"
                    size="mini"
                    type="date"
                    placeholder="选择日期"
                    :picker-options="expireTimeOPtion"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="备注" class="inlineText">
                  <el-input
                    v-model="inStockForm.remark"
                    type="textarea"
                    :rows="3"
                    :disabled="disabled"
                    placeholder="备注"
                    size="mini"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="inStockForm.rejectReason">
              <el-col :span="24">
                <el-form-item label="拒绝原因" class="inlineText mt8">
                  <el-input
                    v-model="inStockForm.rejectReason"
                    type="textarea"
                    :rows="3"
                    :disabled="disabled"
                    placeholder="备注"
                    size="mini"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="inStockForm.disableReason">
              <el-col :span="24">
                <el-form-item label="作废原因" class="inlineText mt8">
                  <el-input
                    v-model="inStockForm.disableReason"
                    type="textarea"
                    :rows="3"
                    :disabled="disabled"
                    placeholder="备注"
                    size="mini"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div class="tableBox">
            <div class="tt flex flex_between">
              <span class="fw">明细</span>
            </div>
            <el-table
              :data="inStockForm.detailList"
              :row-key="(row) => row.rowKey"
              border
              style="width: 100%"
            >
              <el-table-column type="index" label="序号" width="50" />
              <el-table-column
                prop="supplierGoodsNumber"
                width="120"
                align="center"
                label="供应商料号"
              >
                <template slot-scope="{ row }">
                  {{row.supplierGoodsNumber || '请先选择物料'}}
                </template>
              </el-table-column>
              <el-table-column
                prop="school"
                width="160"
                align="center"
                label="物料名称"
              >
                <template slot-scope="scope">
                  <el-select
                    filterable
                    v-model="scope.row.goodsCode"
                    placeholder="请选择物料"
                    size="small"
                    :disabled="disabled"
                    @change="
                      handleMaterialNameChange(scope.row, scope.$index, $event)
                    "
                  >
                    <el-option
                      :label="item.goodsName"
                      :value="item.goodsCode"
                      v-for="(item, index) in materialList"
                      :key="index"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                prop="taxRate"
                width="90"
                align="center"
                label="税率"
              >
                <template slot-scope="{ row }">
                  <el-input
                    :disabled="disabled"
                    v-model="row.taxRate"
                    placeholder="请输入"
                    size="mini"
                  />
                </template>
              </el-table-column>
              <el-table-column
                prop="goodsPriceCurrent"
                width="120"
                align="center"
                label="含税单价"
              >
                <template slot-scope="{ row,$index }">
                  <el-input
                    :disabled="disabled"
                    v-model="row.goodsPriceCurrent"
                    @blur="handlePriceCurrentBlur(row,$index)"
                    placeholder="请输入"
                    size="mini"
                  />
                </template>
              </el-table-column>
              <el-table-column
                prop="goodsPriceCurrentBeforeTax"
                width="100"
                align="center"
                label="未税单价"
              >
                <template slot-scope="{ row,$index }">
                  {{row.goodsPriceCurrentBeforeTax.toFixed(2)}}
                </template>
              </el-table-column>
              <el-table-column
                prop="goodsType"
                width="90"
                align="center"
                label="物料类型"
              >
                <template slot-scope="scope">
                  {{ scope.row.goodsTypeName || "--" }}
                </template>
              </el-table-column>
              <el-table-column
                prop="usepriceUnitName"
                width="90"
                align="center"
                label="使用单位"
              >
              </el-table-column>
              <el-table-column
                prop="priceUnitName"
                width="90"
                align="center"
                label="计价单位"
              >
              </el-table-column>
              <el-table-column
                prop="goodsPriceLast"
                width="90"
                align="center"
                label="原单价"
              >
              </el-table-column>
              <el-table-column
                prop="goodsPriceLastTime"
                width="90"
                align="center"
                label="原核准日"
              >
              </el-table-column>
              <el-table-column prop="remark" align="center" label="备注" min-width="160">
                <template slot-scope="{ row }">
                  <el-input
                    :disabled="disabled"
                    v-model="row.remark"
                    placeholder="请输入"
                    size="mini"
                  />
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                width="100px"
                fixed="right"
                align="center"
                v-if="!disabled"
              >
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    v-if="inStockForm.detailList.length > 1"
                    @click="handleDeleteRow(scope.row, scope.$index)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <div v-if="state == 'arrival'">
          <el-button size="mini" @click="isAddPurDialog = false"
            >取 消</el-button
          >
          <el-button
            :loading="addLableLoading"
            :disabled="addLableLoading"
            element-loading-spinner="el-icon-loading"
            type="success"
            size="mini"
            @click="handleAdopt"
            >通过</el-button
          >
          <el-button
            :loading="addLableLoading"
            :disabled="addLableLoading"
            element-loading-spinner="el-icon-loading"
            type="warning"
            size="mini"
            @click="handleRefuse"
            >拒绝</el-button
          >
        </div>
        <el-button
          v-if="this.state == 'see'"
          size="mini"
          @click="isAddPurDialog = false"
          >取 消</el-button
        >
        <div v-if="this.state != 'see' && state != 'arrival'">
          <el-button size="mini" @click="isAddPurDialog = false"
            >取 消</el-button
          >
          <el-button
            :loading="addPurLoading"
            :disabled="addPurLoading"
            element-loading-spinner="el-icon-loading"
            type="primary"
            size="mini"
            @click="savaAdd"
            >保存</el-button
          >

          <el-button
            :loading="addPurLoading"
            :disabled="addPurLoading"
            v-if="state != 'confirm'"
            element-loading-spinner="el-icon-loading"
            type="primary"
            size="mini"
            @click="savaAdd('confirm')"
            >保存并确认</el-button
          >
        </div>
      </div>
    </el-dialog>

    <!-- 拒绝原因   -->
    <el-dialog
      :title="title + '原因'"
      :visible.sync="reasonVisible"
      :close-on-click-modal="false"
      custom-class="comDialog"
      append-to-body
      width="30%"
      @close="closeCancelDialog"
    >
      <div class="dialogDody">
        <el-form
          ref="cancelForm"
          :model="inStockState"
          :rules="cancelRules"
          label-width="0px"
          class="dialogForm"
        >
          <el-form-item label="" prop="reason">
            <el-input
              v-model="inStockState.reason"
              type="textarea"
              :autosize="{ minRows: 3 }"
              placeholder="请输入内容"
              size="mini"
              clearable
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="reasonVisible = false">取 消</el-button>
        <el-button
          :loading="addLableLoading"
          :disabled="addLableLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="saveReason"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
      <script>
import naTable from "@/components/naTable";
import Pagination from "@/components/Pagination";
import { arraySort } from "@/utils";
import { mapGetters } from "vuex";
import customInput from "@/components/customInput";
export default {
  name: "materialModifyPrice",
  components: {
    Pagination,
    naTable,
    customInput,
  },
  computed: {
    ...mapGetters(["name"]),
    modelTitle: function () {
      let title = "";
      if (this.state == "add") {
        title = "新增";
      } else if (this.state == "edit") {
        title = "编辑";
      } else if (this.state == "see") {
        title = "查看";
      } else if (this.state == "confirm") {
        title = "确认";
      } else if (this.state == "withdraw") {
        title = "撤回";
      } else if (this.state == "arrival") {
        title = "审核";
      }
      return title;
    },
  },
  data() {
    return {
      ops: "add", // 表格操作
      state: "",
      addPurLoading: false,
      isPurInfo: false, // 采购单明细
      expireTimeOPtion: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7; //如果没有后面的-8.64e7就是不可以选择今天的
        },
      },
      disabled: false,
      isAddPurDialog: false, // 新建采购弹框是否打开
      isSeePurDialog: false, // 查看出库详情
      searchLoading: false, // 列表搜索加载效果
      detailLoading: false, //申请单明细加载效果
      marLoading: false, // 物料明细加载效果
      inStockForm: {
        goodsPriceAdjustmentID: 0,
        goodsPriceAdjustmentCode: "",
        goodsPriceAdjustmentNumber: "",
        goodsPriceAdjustmentState: 0,
        supplierCode: "",
        supplierName: "",
        startTime: "",
        remark: "",
        isEffective: 0,
        rejectReason: "",
        disableReason: "",
        auditTime: null,
        auditor: "",
        auditorName: "",
        createTime: null,
        createor: "",
        createorName: "",
        uploadTime: null,
        uploador: "",
        detailList: [],
      }, // 新建核价表单

      rules: {
        startTime: [
          {
            required: true,
            message: "请选择日期",
            trigger: ["change", "blur"],
          },
        ],
        supplierCode: [
          {
            required: true,
            message: "请选择供应商",
            trigger: ["change", "blur"],
          },
        ],
      },

      inStockState: {
        goodsPriceAdjustmentCode: "",
        goodsPriceAdjustmentState: 0,
        reason: "",
        auditor: "",
      },
      title: "",
      reasonVisible: false,
      addLableLoading: false,
      cancelRules: {
        reason: [
          {
            required: true,
            message: "请填写原因",
            trigger: ["change", "blur"],
          },
        ],
      },

      inStockBillDetailList: [], // 出库明细数据
      purInfoLists: [], // 采购明细列表
      inStockID: null, // 出库单ID
      // 出库列表搜索条件
      search: {
        pageIndex: 1,
        pageSize: 30,
        queryDataBaseFiled: "",
        likeWords: "",
        isShowDisabled: 0,
        state: 0,
      },
      detail: [],
      total: 0,
      warhouseList: [], // 仓库下拉列表数据
      materialList: [], // 物料明细列表
      materalSearch: {
        pageIndex: 1,
        pageSize: 30,
        likeWords: "",
      }, // 物料明细搜索条件
      purSearch: {
        pageIndex: 1,
        pageSize: 30,
        queryDataBaseFiled: "",
        likeWords: "",
        isShowDisabled: 0,
        associationBillNo: "",
      }, // 申请单明细搜索条件
      marTotale: 0,
      currentRow: {},
      allSupliers: [],

      tableData: {
        headId: "head", //表头列显隐组件所需ID-避免多个页面id重复问题
        tableId: "inStockID", //表格列表key,也就是一条数据的唯一标识
        senior: false, //高级搜索是否显示
        disableWord: "", //是否禁用显示的字段
        disableStatus: "", //判断未禁用的状态值
        addTitle: "新建", //新建按钮名称-为''则不显示，为undefine则为新建
        addPermission: "MenuCode5-14-1", //新建按钮权限
        addIcon: "", //新建按钮icon
        voidWord: "isShowDisabled", //需要查询作废的字段
        statusList: [], //状态筛选条件列表
        statusWord: "", //状态筛选的字段
        refresh: true, //刷新按钮
        showSum: false, //显示合计
        //表格按钮
        operationBtn: [
          {
            title: "查看", //按钮名称
            methodsName: "seeInsock", //按钮绑定方法的字段
          },
          {
            title: "编辑", //按钮名称
            methodsName: "handleEdit", //按钮绑定方法的字段
            permission: "MenuCode5-14-2", //按钮权限
            hideCondition: {
              //按钮显隐条件
              prop: "goodsPriceAdjustmentState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待确认",
              ],
            },
          },
          {
            title: "确认", //按钮名称
            methodsName: "handleConfirm", //按钮绑定方法的字段
            permission: "MenuCode5-14-3", //按钮权限
            hideCondition: {
              //按钮显隐条件
              prop: "goodsPriceAdjustmentState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待确认",
              ],
            },
          },
          {
            title: "撤回", //按钮名称
            methodsName: "handleWithdraw", //按钮绑定方法的字段
            permission: "MenuCode5-14-4", //按钮权限
            hideCondition: {
              //按钮显隐条件
              prop: "goodsPriceAdjustmentState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待审核",
              ],
            },
          },
          {
            title: "审核", //按钮名称
            methodsName: "arrivalClick", //按钮绑定方法的字段
            permission: "MenuCode5-14-5", //按钮权限
            hideCondition: {
              //按钮显隐条件
              prop: "goodsPriceAdjustmentState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待审核",
              ],
            },
          },
          {
            title: "作废", //按钮名称
            methodsName: "handleVoid", //按钮绑定方法的字段
            permission: "MenuCode5-14-6", //按钮权限
            hideCondition: {
              //按钮显隐条件
              prop: "goodsPriceAdjustmentState", //控制显隐的状态字段

              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待确认",
              ],
            },
          },
          {
            title: "打印核价单", //按钮名称
            methodsName: "handleDownload", //按钮绑定方法的字段
          },
        ],
        //表格内容
        tbody: [],
        //表格表头
        thead: [],
      },
    };
  },
  created() {
    // 采购列表表格头部
    this.getTableHead();

    //获取供应商
    this.getSupplier();
  },
  mounted() {},
  methods: {
    // 供应商选择改变事件
    supplierChange(val) {
      this.inStockForm.detailList = [];
      this.inStockForm.detailList.push({
        goodsPriceAdjustmentDetailID: 0,
        goodsPriceAdjustmentDetailCode: "",
        goodsPriceAdjustmentCode: "",
        supplierGoodsNumber: "",
        goodsCode: "",
        goodsName: "",
        goodsTypeName: "",
        goodsUnitName: "",
        priceUnitName: "",
        goodsPriceLast: 0,
        goodsPriceLastTime: "",
        goodsPriceCurrent: 0,
        remark: "",
        goodsPriceCurrentBeforeTax:0,
        taxRate:0,
        createTime: this.getDate(),
        createor: "",
        rowKey: Date.now(),
      });

      this.$api.mater
        .GetGoodsSpecificationBySupplierCode({
          queryBy: 0,
          queryValue: val,
        })
        .then((res) => {
          this.materialList = res;
        });
      this.$forceUpdate();
    },

    // 获取供应商
    getSupplier() {
      this.$api.mater.GetAllSuppliers().then((res) => {
        this.allSupliers = res;
      });
    },

    closeCancelDialog() {
      this.inStockState.reason = "";
    },

    //物料下拉选择事件
    handleMaterialNameChange(row, index, e) {
      if (index == this.inStockForm.detailList.length - 1) {
        this.inStockForm.detailList.push({
          goodsPriceAdjustmentDetailID: 0,
          goodsPriceAdjustmentDetailCode: "",
          goodsPriceAdjustmentCode: "",
          supplierGoodsNumber: "",
          goodsCode: "",
          goodsName: "",
          goodsTypeName: "",
          goodsUnitName: "",
          priceUnitName: "",
          goodsPriceLast: 0,
          goodsPriceLastTime: "",
          goodsPriceCurrent: 0,
          goodsPriceCurrentBeforeTax:0,
        taxRate:0,
          remark: "",
          createTime: this.getDate(),
          createor: "",
          rowKey: Date.now(),
        });
      }
      row.stockGoodsCode = e;
      this.materialList.forEach((item, i) => {
        if (item.goodsCode == e) {
          row.goodsTypeName = item.goodsType;
          row.usepriceUnitName = item.goodsUnit;
          row.supplierGoodsNumber = item.goodsCode;
          row.priceUnitName = item.priceUnit;
          this.$set(row, "goodsPriceLast", item.goodsPrice);
          // row.goodsPriceLast = item.goodsPrice;
          row.goodsPriceLastTime = item.goodsPriceAdjustmentTime;
        }
      });
    },

    //编辑
    handleEdit(row) {
      this.state = "edit";
      this.disabled = false;
      this.getStockBillInfo(row.goodsPriceAdjustmentCode);
    },

    // 查看
    seeInsock(row) {
      this.state = "see";
      this.disabled = true;
      this.getStockBillInfo(row.goodsPriceAdjustmentCode);
    },

    //确认
    handleConfirm(row) {
      this.state = "confirm";
      this.disabled = true;
      this.inStockState.goodsPriceAdjustmentCode = row.goodsPriceAdjustmentCode;
      this.getStockBillInfo(row.goodsPriceAdjustmentCode);
    },

    //含税单价
    handlePriceCurrentBlur(row,index){
     let rowData = this.inStockForm.detailList[index]
      rowData.goodsPriceCurrentBeforeTax = Number(rowData.goodsPriceCurrent)/(1 + Number(rowData.taxRate))
      console.log(row)
      // row.goodsPriceCurrentBeforeTax = row.goodsPriceCurrent*(1-row.taxRate)
    },

    //未税单价
    handleCurrentBeforeTaxBlur(row,index){
      let rowData = this.inStockForm.detailList[index]
      rowData.goodsPriceCurrent = Number(rowData.goodsPriceCurrentBeforeTax)*(1+Number(rowData.taxRate))
    },

    //撤回
    handleWithdraw(row) {
      this.state = "withdraw";
      this.disabled = true;
      this.inStockState.goodsPriceAdjustmentCode = row.goodsPriceAdjustmentCode;
      this.getStockBillInfo(row.goodsPriceAdjustmentCode);
    },

    //通过
    handleAdopt() {
      this.inStockState.goodsPriceAdjustmentState = 3;
      this.saveState();
    },

    //拒绝
    handleRefuse() {
      this.title = "拒绝";
      this.inStockState.goodsPriceAdjustmentState = 2;
      this.reasonVisible = true;
    },

    //审核
    arrivalClick(row) {
      this.state = "arrival";
      this.disabled = true;
      this.inStockState.goodsPriceAdjustmentCode = row.goodsPriceAdjustmentCode;
      this.getStockBillInfo(row.goodsPriceAdjustmentCode);
    },

    //作废
    handleVoid(row) {
      this.title = "作废";
      this.inStockState.goodsPriceAdjustmentCode = row.goodsPriceAdjustmentCode;
      this.inStockState.goodsPriceAdjustmentState = 4;
      this.reasonVisible = true;
    },

    //保存原因
    saveReason() {
      this.$refs.cancelForm.validate((valid) => {
        if (valid) {
          this.saveState();
        }
      });
    },

    //删除明细
    handleDeleteRow(row, index) {
      this.inStockForm.detailList.splice(index, 1);
    },

    //其他出库
    handleOtherInstockBill() {
      this.state = "add";
      this.materialList = [];
      this.disabled = false;
      this.inStockForm.createTime = this.getDate();
      this.inStockForm.detailList.push({
        goodsPriceAdjustmentDetailID: 0,
        goodsPriceAdjustmentDetailCode: "",
        goodsPriceAdjustmentCode: "",
        supplierGoodsNumber: "",
        goodsCode: "",
        goodsName: "",
        goodsTypeName: "",
        goodsUnitName: "",
        priceUnitName: "",
        goodsPriceLast: 0,
        goodsPriceLastTime: "",
        goodsPriceCurrent: 0,
        goodsPriceCurrentBeforeTax:0,
        taxRate:0,
        remark: "",
        createTime: "",
        createor: "",
        rowKey: Date.now(),
      });
      this.isAddPurDialog = true;
    },

    // 出库单详情
    getStockBillInfo(id) {
      let obj = {
        queryBy: 0,
        queryValue: id,
      };
      this.$api.warehouse.GetGoodsPriceAdjustmentByCode(obj).then((res) => {
        this.inStockForm = res;
        this.inStockForm.detailList = res.detailList;
        this.$api.mater
          .GetGoodsSpecificationBySupplierCode({
            queryBy: 0,
            queryValue: this.inStockForm.supplierCode,
          })
          .then((data) => {
            this.materialList = data;
            this.inStockForm.detailList.forEach((k, i) => {
              this.materialList.forEach((item) => {
                if (item.goodsCode == k.goodsCode) {
                  k.goodsTypeName = item.goodsType;
                  k.usepriceUnitName = item.goodsUnit;
                  k.priceUnitName = item.priceUnit;
                  k.goodsPriceLastTime = item.goodsPriceAdjustmentTime;
                }
              });
            });
          });
        this.isAddPurDialog = true;
      });
    },

    //下载核价单
    handleDownload(row) {
      let parmas = {
        templateTypeCode: "MBLX0009", //	模板类型编号
        customerCode: row.customerCode,
        condition: {
          GoodsPriceAdjustmentCode: row.goodsPriceAdjustmentCode,
        }, //查询条件
      };
      this.$api.system.DownLoadTemplate(parmas).then((res) => {
        window.open(res);
      });
    },

    //保存状态
    saveState() {
      this.addLableLoading = true;
      this.$api.warehouse
        .UpdateGoodsPriceAdjustmentState(this.inStockState)
        .then((res) => {
          this.addPurLoading = false;
          this.isAddPurDialog = false;
          this.addLableLoading = false;
          this.reasonVisible = false;
          this.getAllLists();
          this.$message.success("保存成功");
        })
        .catch(() => {
          this.addPurLoading = false;
          this.isAddPurDialog = false;
          this.addLableLoading = false;
          this.reasonVisible = false;
        });
    },

    // 新建
    savaAdd(type) {
      this.$refs.inStockForm.validate((valid) => {
        if (valid) {
          this.addPurLoading = true;
          let arr = JSON.parse(JSON.stringify(this.inStockForm.detailList));
          let state = true;
          if (this.state == "add" || this.state == "edit") {
            if (arr.length > 1) {
              arr.pop();
            }
          }

          arr.forEach((item) => {
            if (!item.supplierGoodsNumber || !item.goodsCode) {
              state = false;
            }
          });

          if (!state) {
            this.$message.warning("请填写供应商料号或物料名称");
            this.addPurLoading = false;
            return;
          }

          this.inStockForm.detailList = JSON.parse(JSON.stringify(arr));
          const parmas = this.inStockForm;
          if (this.state == "add") {
            this.inStockForm.detailList.forEach((item) => {
              item.createTime = this.getDate();
            });
            this.$api.warehouse
              .AddGoodsPriceAdjustment(parmas)
              .then((res) => {
                if (type === "confirm") {
                  this.inStockState.goodsPriceAdjustmentState = 1;
                  this.inStockState.goodsPriceAdjustmentCode = res;
                  this.$api.warehouse
                    .UpdateGoodsPriceAdjustmentState(this.inStockState)
                    .then((res) => {
                      this.addPurLoading = false;
                      this.isAddPurDialog = false;
                      this.getAllLists();
                      this.$message.success("保存成功");
                    })
                    .catch(() => {});
                }
              })
              .catch(() => {
                this.addPurLoading = false;
                this.inStockForm.detailList.push({
                  goodsPriceAdjustmentDetailID: 0,
                  goodsPriceAdjustmentDetailCode: "",
                  goodsPriceAdjustmentCode: "",
                  supplierGoodsNumber: "",
                  goodsCode: "",
                  goodsName: "",
                  goodsTypeName: "",
                  goodsUnitName: "",
                  goodsPriceCurrentBeforeTax:0,
        taxRate:0,
                  priceUnitName: "",
                  goodsPriceLast: 0,
                  goodsPriceLastTime: "",
                  goodsPriceCurrent: 0,
                  remark: "",
                  createTime: "",
                  createor: "",
                  rowKey: Date.now(),
                });
              });
          } else if (this.state == "edit") {
            this.$api.warehouse
              .UpdateGoodsPriceAdjustment(parmas)
              .then(() => {
                if (type === "confirm") {
                  this.inStockState.goodsPriceAdjustmentState = 1;
                  this.inStockState.goodsPriceAdjustmentCode =
                    parmas.goodsPriceAdjustmentCode;
                  this.$api.warehouse
                    .UpdateGoodsPriceAdjustmentState(this.inStockState)
                    .then((res) => {
                      this.addPurLoading = false;
                      this.isAddPurDialog = false;
                      this.getAllLists();
                      this.$message.success("保存成功");
                    })
                    .catch(() => {});
                }
              })
              .catch(() => {
                this.inStockForm.detailList.push({
                  goodsPriceAdjustmentDetailID: 0,
                  goodsPriceAdjustmentDetailCode: "",
                  goodsPriceAdjustmentCode: "",
                  supplierGoodsNumber: "",
                  goodsCode: "",
                  goodsName: "",
                  goodsTypeName: "",
                  goodsUnitName: "",
                  priceUnitName: "",
                  goodsPriceCurrentBeforeTax:0,
        taxRate:0,
                  goodsPriceLast: 0,
                  goodsPriceLastTime: "",
                  goodsPriceCurrent: 0,
                  remark: "",
                  createTime: "",
                  createor: "",
                  rowKey: Date.now(),
                });
                this.addPurLoading = false;
              });
          } else if (this.state == "confirm") {
            this.inStockState.goodsPriceAdjustmentState = 1;
            this.saveState();
          } else if (this.state == "withdraw") {
            this.inStockState.goodsPriceAdjustmentState = 0;
            this.saveState();
          }
        }
      });
    },
    // 给选中一行添加class
    tableRowClassName({ row }) {
      if (this.currentRow.purchaseOrderNo === row.purchaseOrderNo) {
        return "selectd";
      }
    },

    // 合计方法
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (column.property === "inStockSumValue") {
          const values = data.map((item) => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] = sums[index]++;
          } else {
            sums[index] = " ";
          }
        }
      });
      return sums;
    },

    getDate() {
      return this.dayjs().format("YYYY-MM-DD");
    },
    // 采购列表表格头部
    getTableHead() {
      const parmas = {
        tableCode: this.$config.goodsPriceAdjustment,
      };
      this.$api.common.getAllTableHead(parmas).then((res) => {
        res.sort(arraySort("columnSort"));
        this.tableData.thead = res;
        this.getAllLists();
      });
    },
    // 出库列表
    getAllLists() {
      const parmas = this.search;
      this.searchLoading = true;
      this.$api.warehouse
        .GetPageGoodsPriceAdjustment(parmas)
        .then((res) => {
          res.list.forEach((item) => {
            if (item.goodsPriceAdjustmentState == 0) {
              item.goodsPriceAdjustmentState = "待确认";
            } else if (item.goodsPriceAdjustmentState == 1) {
              item.goodsPriceAdjustmentState = "待审核";
            } else if (item.goodsPriceAdjustmentState == 2) {
              item.goodsPriceAdjustmentState = "已拒绝";
            } else if (item.goodsPriceAdjustmentState == 3) {
              item.goodsPriceAdjustmentState = "已完成";
            } else {
              item.goodsPriceAdjustmentState = "已作废";
            }

            if (item.isEffective == 0) {
              item.isEffective = "否";
            } else {
              item.isEffective = "是";
            }
          });
          this.tableData.tbody = res.list;
          this.search.total = res.totalCount;
          this.searchLoading = false;
        })
        .catch(() => {
          this.searchLoading = false;
        });
    },
    // 物料明细弹框关闭事件
    closeMarDialog() {
      this.materalSearch = this.$options.data().materalSearch;
    },
    // 关闭弹框事件
    closeDialog() {
      // 关闭弹框的时候清空数据
      if (this.$refs.inStockForm) {
        this.$refs.inStockForm.resetFields();
      }
      if (this.$refs.editInStockForm) {
        this.$refs.editInStockForm.resetFields();
      }
      this.inStockForm = this.$options.data().inStockForm;
      this.inStockBillDetailList = this.$options.data().inStockBillDetailList;
    },
  },
};
</script>
      <style lang="scss" scoped>
.addWuliaoBox {
  padding: 10px 16px;

  .tableBox {
    margin-top: 16px;
    border: 1px solid #e6e6e6;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    overflow: hidden;

    .tt {
      height: 38px;
      line-height: 38px;
      padding-left: 16px;
      background: #f7f8fa;
      border-radius: 8px 8px 0 0;
    }
  }
}

.subTip {
  border-bottom: 1px #e6e6e6 solid;
  background: #f5f6f9;
  padding: 8px 16px;
}

.paginaCon.noborder {
  border-top: none;
  text-align: left;

  .pagination-container {
    padding: 0;
  }
}

.marDialog {
  height: 100%;

  .btnCon {
    border-bottom: none;
  }
}
</style>
